package edu.gatech.oad.user;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import edu.gatech.oad.doctorsorder.ChartPanel;
import edu.gatech.oad.doctorsorder.IncomeStatPanel;
import edu.gatech.oad.records.PatientHistoryPanel;
import edu.gatech.oad.records.TreatmentRecord;
import edu.gatech.oad.schedule.Appointment;
import edu.gatech.oad.schedule.AppointmentPanel;
import edu.gatech.oad.schedule.AppointmentViewPanel;
import edu.gatech.oad.schedule.CalendarController;
import edu.gatech.oad.ui.UserPanel;

public class UserProfilePanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private User currentUser, profileUser;
	private JPanel profilePanel, buttonPanel;
	private UserPanel userPanel;
	private List<String> buttonTitles = new ArrayList<String>();
	private Appointment appt;
	private List<JButton> buttons = new ArrayList<JButton>();


	private UserProfilePanel() {
		this.setLayout(new GridLayout(2, 1));
		this.setPreferredSize(new Dimension(800, 500));
		buttonPanel = new JPanel();
		buttonPanel.setPreferredSize(new Dimension(150, 150));
		buttonPanel.setLayout(new GridLayout(2, 4));
		profilePanel = new JPanel();
		profilePanel.setPreferredSize(new Dimension(150, 150));

		add(profilePanel);
		add(buttonPanel);
	}

	public UserProfilePanel(User currentUser, UserPanel userPanel,
			User profileUser) {
		this();
		this.currentUser = currentUser;
		this.profileUser = profileUser;
		this.userPanel = userPanel;
		addProfilePanel();
		addButtonPanel();
	}

	private void addProfilePanel() {
		if (currentUser.equals(profileUser))
			profilePanel.setBorder(new TitledBorder("Welcome "
					+ profileUser.getFirstName()));
		else
			profilePanel.setBorder(new TitledBorder(profileUser.getName()));
	}

	private void addButtonPanel() {
		buttonPanel.setBorder(new TitledBorder("What would you like to do?"));
		if (profileUser instanceof Patient) {
			buttonTitles.add("Edit Profile");
			buttonTitles.add("View Patient History");
			buttonTitles.add("Appointments");
			buttonTitles.add("View Chart");
			

			if(currentUser instanceof Doctor | currentUser instanceof SystemAdministrator){
				if(!currentUser.equals(profileUser)){
					buttonTitles.add("Delete User");
				}
			} else if(currentUser instanceof Patient){
				buttonTitles.add("Check In");
			}
			for (int i = 0; i < buttonTitles.size(); i++) {
				JButton button = new JButton(buttonTitles.get(i));
				button.addActionListener(new ButtonListener());
				button.setActionCommand(buttonTitles.get(i));
				buttonPanel.add(button);
				if(button.getActionCommand().equals("Check In")){
					for(Appointment appts : ((Patient)profileUser).getAppointments()){
						if(!appts.withinThirtyMinutes()){
							button.setEnabled(false);
						} else {
							button.setEnabled(true);
							this.appt = appts.nextAppointment();
						}
					}
				}
				buttons.add(button);
				updateUI();
			}
		}
		
		if (profileUser instanceof MedicalPersonnel) {
			String[] buttonTitles = { "Edit Profile", "View Schedule",
					"Edit Schedule","View Income Statement" };
			for (int i = 0; i < buttonTitles.length; i++) {
				JButton button = new JButton(buttonTitles[i]);
				button.addActionListener(new ButtonListener());
				button.setActionCommand(buttonTitles[i]);
				buttonPanel.add(button);
				updateUI();
			}
		}
	}

	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals("View Patient History")) {
				userPanel.addNewTab(profileUser.getLastName()
						+ "'s Patient History", new PatientHistoryPanel(
						((Patient) profileUser).getPatientHistory(),
						currentUser, userPanel));
			}
			if (e.getActionCommand().equals("Edit Profile")) {
				userPanel.addNewTab("Edit Profile", new UserEditPanel(
						profileUser, currentUser, userPanel));
			}
			if (e.getActionCommand().equals("Appointments")) {
				userPanel.addNewTab(profileUser.getLastName()
						+ "'s Appointments", new AppointmentViewPanel(
						(Patient) profileUser, userPanel));
			}
			if (e.getActionCommand().equals("View Chart")) {
				userPanel.addNewTab(profileUser.getLastName()
						+ "'s Charts", new ChartPanel(
						(Patient) profileUser));
			}
			if(e.getActionCommand().equals("View Income Statement")){
				GregorianCalendar currentDate=new GregorianCalendar();
				userPanel.addNewTab("Income Statement", 
						new IncomeStatPanel(AccountsController.sharedController().getPatients(), 
						currentDate.get(Calendar.MONTH)+1, currentDate.get(Calendar.YEAR)));
			}
			if(e.getActionCommand().equals("Delete User")){
				int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete the user?", "Delete User", JOptionPane.YES_NO_OPTION);
				if(option == JOptionPane.YES_OPTION){
					AccountsController.sharedController().removeUserForUsername(profileUser.getUsername());
					userPanel.getTabbedPane().remove(userPanel.getTabbedPane().getTabCount()-1);
				}
			}	
			if(e.getActionCommand().equals("Check In")){
				CalendarController.sharedCalendarController().checkIn((Patient)profileUser, appt);
				TreatmentRecord treatmentRecord = new TreatmentRecord(appt);
				appt.getPatient().getPatientHistory().addTreatmentRecord(treatmentRecord);
				buttons.get(buttons.size()-1).setEnabled(false);
			}
		}

	}
}
